package com.ssi.action.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;
import com.ssi.action.CommonAction;
import com.ssi.domain.user.User;
import com.ssi.service.user.UserService;

/**
 * UserAction
 * 
 * @author : 
 * @version :
 */

public class UserAction extends CommonAction {
	private UserService userService;
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	String json;
	public String getJson() {
		return json;
	}
	public void setJson(String json) {
		this.json = json;
	}
	public String loginPage(){
		return "loginPage";
	}
	public String login(){
		String userName = getParameters("username");
		String password =getParameters("password");
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("userName",userName);
		map.put("password",password);
		User user = userService.login(map);
		JSONObject jsonObject=new JSONObject();
		if(user!=null){
			System.out.println("登陆成功");
			setSessionAttribute("user", user);
			jsonObject.put("msg", 1);
			jsonObject.put("user", user);
			
		}else{
			jsonObject.put("msg", 0);
			System.out.println("登陆失败");
		}
		json=jsonObject.toString();
		return "json";
		
	}
	
	public String userIndex(){
		
		return "userIndex";
	}
	public String addUser() {
		User user=new User();
		userService.addUser(user);
		return "addUser";
	}

	public String deleteUser() {
		User user=new User();
		userService.deleteUser(user.getId());
		return "deleteUser";
	}

	public String updateUser() {
		User user=new User();
		userService.updateUser(user);
		return "updateUser";
	}
	public String queryAllUser() {
		 List<User> queryAllUser = userService.queryAllUser();
		return "userList";
	}



}
